import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();
Page({
  data: {
    selectFacScope: {},
    nowDate: "",
    selectnowDate: '',
    recordDate: "",
    showDate: "",
    showDateList: [],
    row: 0,
    shiftWid1: 30,
    shiftWid2: 25,
    beginDate: '',
    endDate: '',
    plinList: [],
  },

  onLoad() {
    let that = this
    this.data.beginDate = dayjs().startOf('month').format('YYYY-MM-DD');
    this.data.endDate = dayjs().endOf('month').format('YYYY-MM-DD');
    this.data.nowDate = dayjs().format('YYYY-MM-DD');
    this.data.selectnowDate = dayjs().format('YYYY-MM-DD');
    this.data.showSelectnowDate = dayjs().format('YYYY年MM月DD日');
    this.data.recordDate = dayjs().format('YYYY-MM-DD');
    this.data.showDate = dayjs().format('YYYY-MM');
    this.setData({
      showDate: this.data.showDate
    })
    let dateArr = this.data.nowDate.split('-')
    // 每月第一天日期
    this.data.dateArrval = dateArr[0] + "-" + dateArr[1] + "-01"
  },

  onShow() {
    let that = this

    // let plinList = [{
    //   "ftyModelId": 3593,
    //   "name": "测试2",
    //   "beginDatetime": "2020-02-20 03:03:00",
    //   "description": "测试2",
    //   "id": 103,
    //   "ftyModelValue": 1,
    //   "type": 1,
    //   "endDatetime": "2020-02-22 04:01:00",
    //   "ftyModelName": "DEMO演示工厂"
    // }, {
    //   "ftyModelId": 3593,
    //   "name": "0915",
    //   "beginDatetime": "2020-02-21 09:00:00",
    //   "description": "",
    //   "id": 104,
    //   "ftyModelValue": 1,
    //   "type": 1,
    //   "endDatetime": "2020-02-21 23:59:59",
    //   "ftyModelName": "DEMO演示工厂"
    // }]

    dd.getStorage({
      key: 'selectFacScope',
      success: function (res) {
        that.setData({
          selectFacScope: res.data[0]
        })
        that.getFactoryCalendarQueryDayType(that.data.dateArrval)
        that.getFactoryCalendarGetbydate()
      },
    });
  },

  // 获取给定时间段内的⼯⼚⽇历⽇类型
  getFactoryCalendarQueryDayType(val) {
    let that = this
    $http(apiSetting.factoryCalendarQueryDayType, {
      beginDate: that.data.beginDate,
      endDate: that.data.endDate,
      ftyModelId: that.data.selectFacScope.key,
      ftyModelValue: that.data.selectFacScope.level + 1
    }).then((data) => {
      that.data.resultList = data.result
      that.formatData(val)
    })
  },

  getFactoryCalendarGetbydate() {
    let that = this
    $http(apiSetting.factoryCalendarGetbydate, {
      date: this.data.selectnowDate,
      ftyModelId: that.data.selectFacScope.key,
      ftyModelValue: that.data.selectFacScope.level + 1
    }).then((data) => {
      let plinList1 = data.result.plannedShutdownMgtDTOList
      let plinList2 = data.result.shiftMgtDTOList
      for (let i in plinList1) {
        let beginDatetimeArr = dayjs(plinList1[i].beginDatetime).format('HH:mm:ss').split(":")
        let endDatetimeArr = dayjs(plinList1[i].endDatetime).format('HH:mm:ss').split(":")
        // 如果是指定星期
        if (plinList1[i].dayOfWeek) {
          beginDatetimeArr = plinList1[i].beginTime.split(":")
          endDatetimeArr = plinList1[i].endTime.split(":")
        }
        let num1 = parseInt(beginDatetimeArr[0]) * 60 + parseInt(beginDatetimeArr[1])
        let num2 = parseInt(endDatetimeArr[0]) * 60 + parseInt(endDatetimeArr[1])
        plinList1[i].timeNum1 = beginDatetimeArr[0] + ":" + beginDatetimeArr[1]
        plinList1[i].timeNum2 = endDatetimeArr[0] + ":" + endDatetimeArr[1]
        let wid = (num2 - num1) / (24 * 60)
        let left = (num1) / (24 * 60)
        if (wid > 1) {
          wid = 1
          wid = wid - left
        }
        if (left > 1) {
          left = 1
        }
        plinList1[i].wid = wid * 100
        plinList1[i].left = left * 100
        plinList1[i].shutDown = true
      }
      for (let i in plinList2) {
        let beginDatetimeArr = plinList2[i].beginTime.split(":")
        let endDatetimeArr = plinList2[i].endTime.split(":")
        let num1 = parseInt(beginDatetimeArr[0]) * 60 + parseInt(beginDatetimeArr[1])
        let num2 = parseInt(endDatetimeArr[0]) * 60 + parseInt(endDatetimeArr[1])
        plinList2[i].timeNum1 = beginDatetimeArr[0] + ":" + beginDatetimeArr[1]
        plinList2[i].timeNum2 = endDatetimeArr[0] + ":" + endDatetimeArr[1]
        let wid = (num2 - num1) / (24 * 60)
        let left = (num1) / (24 * 60)
        if (wid > 1) {
          wid = 1
          wid = wid - left
        }
        if (left > 1) {
          left = 1
        }
        plinList2[i].wid = wid * 100
        plinList2[i].left = left * 100
      }
      that.setData({
        plinList: [...plinList2, ...plinList1]
      })
      console.log(this.data.plinList)
    })
  },

  formatData(date) {
    let dateArr = this.data.recordDate.split('-')
    let week = dayjs(date).day()
    let days = dayjs(date).daysInMonth()
    let sum = week + days
    let row = Math.ceil(sum / 7)
    let dataList = []
    for (let i = 0; i < sum; i++) {
      if (i >= week) {
        let val = i - (week - 1)
        let obj = {
          title: val,
          value: dayjs(dateArr[0] + "-" + dateArr[1] + '-' + val).format('YYYY-MM-DD')
        }
        if (obj.value == this.data.nowDate) {
          obj.today = true
          obj.selectDay = true
        }
        dataList.push(obj)
      } else {
        let obj = {
          title: '',
          value: -1
        }
        dataList.push(obj)
      }
    }

    for (let i = 0; i < this.data.resultList.length; i++) {
      let findin = dataList.findIndex(n => {
        return n.value == this.data.resultList[i].date
      })
      if (findin != -1) {
        dataList[findin].dayType = this.data.resultList[i].dayType
      }
    }
    this.setData({
      row: row,
      showDateList: dataList
    })
  },

  // 月份切换
  cutdata(e) {
    let type = e.target.dataset.type;
    let recordDate = this.data.recordDate;
    let val = ''
    if (type == 'l') {
      val = dayjs(recordDate).subtract(1, 'month');
    }
    if (type == 'r') {
      val = dayjs(recordDate).add(1, 'month');
    }
    this.data.recordDate = dayjs(val).format('YYYY-MM-DD');
    this.data.showDate = dayjs(val).format('YYYY-MM');
    let dateArr = this.data.recordDate.split('-')
    let dateVal = dateArr[0] + "-" + dateArr[1] + "-01"
    this.data.dateArrval = dateVal
    this.setData({
      showDate: this.data.showDate
    })
    this.showDateList = []
    this.data.beginDate = dayjs(val).startOf('month').format('YYYY-MM-DD');
    this.data.endDate = dayjs(val).endOf('month').format('YYYY-MM-DD');
    this.getFactoryCalendarQueryDayType(dateVal)
  },

  selectDate(e) {
    let item = e.target.dataset.item
    if (item.value == -1) {
      return
    } else {
      this.data.selectnowDate = item.value
      this.setData({
        showSelectnowDate: dayjs(item.value).format('YYYY年MM月DD日')
      })
    }
    for (let i in this.data.showDateList) {
      if (this.data.showDateList[i].title == item.title) {
        this.data.showDateList[i].selectDay = true
      } else {
        this.data.showDateList[i].selectDay = false
      }
    }
    this.setData({
      showDateList: this.data.showDateList
    })
    this.getFactoryCalendarGetbydate()
  },

  selectRange() {
    dd.navigateTo({
      url: '/pages/selectRange/selectRange?type=back'
    })
  },
});
